#!/bin/bash

if test "f$1" "=" "f"
then 
    echo "Error, please specify a JobID"
    exit 1
fi
. /opt/asf/tools/scripts/arguments hydra.ftp $1
DATE_STAMP=`date +"%Y%m%d%H%M%S"`
JOB_FOLDERS="/home/asf/jobs"
JOB_FOLDER="$JOB_FOLDERS/$1"
JOB_OUTPUT_FOLDER="$JOB_FOLDER/$DATE_STAMP"
TERM="xterm"
if ! test -e "$JOB_FOLDER"
then 
    echo "Error, JobID $1 is invalid"
    exit 1
fi
if test -e "JOB_FOLDER/.lock"
then 
	echo "Error, process is already running"
	exit 1
fi
echo > "$JOB_FOLDER/.lock"
cd /opt/asf/frontend/asfui
. bin/activate
python3 /opt/asf/frontend/asfui/manage.py remaster_input --input JobID:$1 --parser host --output "$JOB_FOLDER/app.input"
mkdir -p "$JOB_OUTPUT_FOLDER/results"
cp /opt/asf/tools/dicts/default.dict "$JOB_FOLDER/app.dict"
cp /opt/asf/tools/dicts/users.dict "$JOB_FOLDER/app.users"
for mode in input asf dict users
do cp -v "$JOB_FOLDER/app.$mode" "$JOB_OUTPUT_FOLDER/app.$mode"
done
echo hydra -o "$JOB_OUTPUT_FOLDER/results/report.txt" -M "$JOB_OUTPUT_FOLDER/app.input" -L "$JOB_OUTPUT_FOLDER/app.users" -P "$JOB_OUTPUT_FOLDER/app.dict" ftp -V -F 2>&1 \> "$JOB_OUTPUT_FOLDER/app.log"
hydra -o "$JOB_OUTPUT_FOLDER/results/report.txt" -M "$JOB_OUTPUT_FOLDER/app.input" -L "$JOB_OUTPUT_FOLDER/app.users" -P "$JOB_OUTPUT_FOLDER/app.dict" ftp -V -F 2>&1 > "$JOB_OUTPUT_FOLDER/app.log" &
JOB_PID=$!
echo $JOB_PID>"$JOB_FOLDER/pid"
wait $PID
python3 /opt/asf/frontend/asfui/manage.py remaster_output --parser=hydra.ftp --debug --input="$JOB_OUTPUT_FOLDER/results/report.txt" --output=JobID:$1 
rm -v "$JOB_FOLDER/pid"
rm -v "$JOB_FOLDER/.lock"